package util;

import java.util.Vector;

public class Validator {
	private String propertyName;
	private String message;
	private Vector issueList;

	private Vector getIssueList() {
		if (issueList == null) {
			issueList = new Vector(1, 1);
		}
		return issueList;
	}

	public Validator addIssue(ValidationIssue issue) {
		getIssueList().addElement(issue);
		return this;
	}

	public boolean hasIssues() {
		if (issueList == null) {
			return false;
		}

		return issueList.size() > 0;
	}

	public String getSummary() {
		if (!hasIssues()) {
			return "";
		}

		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < issueList.size(); i++) {
			ValidationIssue issue = (ValidationIssue) issueList.elementAt(i);
			buffer.append(Util.formatString("- %s.\n", issue.getMessage()));
		}
		return buffer.toString();
	}

	public static Validator addIssue(Validator result, ValidationIssue issue) {
		if (result == null) {
			result = new ValidationResult();
		}

		return result.addIssue(issue);
	}

	public static Validator addIssue(Validator result, String property,
			String message) {
		return addIssue(result, property, message, null);
	}

	public static Validator addIssue(Validator result, String property,
			String message, Object[] arg) {
		return addIssue(result, new ValidationIssue(property, message, arg));
	}

	public String getPropertyName() {
		return propertyName;
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}
